package com.hongye.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongye.common.entity.BrokerEntity;
import com.hongye.common.response.ResponseResult;
import com.hongye.manager.feign.EsServiceFeign;
import com.hongye.manager.mapper.BrokerMapper;
import com.hongye.manager.service.BrokerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/broker")
public class BrokerController {

    @Autowired
    private BrokerMapper brokerMapper;
    @Autowired
    private EsServiceFeign esServiceFeign;

    @Autowired
    private BrokerService service;

    @RequestMapping("/list")
    public ResponseResult list(){
        List<BrokerEntity> list = brokerMapper.selectList(null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        return ResponseResult.ok(1,"成功",map);
    }


    @RequestMapping("/findlist")
    public List<BrokerEntity> findlist(String name){
        QueryWrapper<BrokerEntity> wrapper = new QueryWrapper<>();
        wrapper.like("broker_name",name);
        List<BrokerEntity> findlist = brokerMapper.selectList(wrapper);
        return findlist;
    }

    @RequestMapping("/del")
    public ResponseResult del(Integer id){
        int i = brokerMapper.deleteById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("i",i);
        return ResponseResult.ok(1,"成功",map);
    }

    /**
     * 分页查询
     */
    @RequestMapping("/findPage")
    public List<BrokerEntity> fenye(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "size",defaultValue = "10") int size,@RequestParam String name){
        //创建分页对象
        Page<BrokerEntity> paged = new Page<>(page,size);
        List<BrokerEntity> jobInfos = service.selectUserPage(paged,name);
        return jobInfos;
    }


    @RequestMapping("/add")
    public Object add(@RequestBody BrokerEntity brokerEntity){
        System.out.println(brokerEntity);
        if (service.isExist(brokerEntity)){
            return ResponseResult.error(1,"用户名重复,请重新输入");
        }
        boolean save = service.save(brokerEntity);

        //添加到es中
        esServiceFeign.add(brokerEntity);

        return save;
    }

    @RequestMapping("/update")
    public ResponseResult update(@RequestBody BrokerEntity brokerEntity){

        BrokerEntity broker = new BrokerEntity();
        broker.setId(brokerEntity.getId());
        broker.setBrokerName(brokerEntity.getBrokerName());
        broker.setSex(brokerEntity.getSex());
        broker.setPhone(brokerEntity.getPhone());
        broker.setCompany(brokerEntity.getCompany());
        broker.setBrokerAddress(brokerEntity.getBrokerAddress());
        broker.setNewhomeSales(brokerEntity.getNewhomeSales());
        broker.setSechomeSales(brokerEntity.getSechomeSales());
        broker.setRenthomeSales(brokerEntity.getRenthomeSales());

        int upbyid = brokerMapper.updateById(broker);
        HashMap<String, Object> map = new HashMap<>();
        map.put("upbyid",upbyid);
        return ResponseResult.ok(1,"成功",map);
    }

}
